package com.xlh.virtualization.dao.helper;

import com.xlh.virtualization.dao.ImageMapper;
import com.xlh.virtualization.domain.Image;
import com.xlh.virtualization.domain.example.ImageExample;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

import static com.xlh.virtualization.constant.ImageConstant.TEACHER_MADE_CODE;

/**
 * @author cheer
 */
@Repository
public class ImageMapperHelper {

    @Autowired
    private ImageMapper imageMapper;

    public void deleteByIds(List<Long> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return;
        }
        ImageExample example = new ImageExample();
        example.or().andIdIn(ids)
                .andDeletedEqualTo(false);
        Image image = Image.builder()
                .deleted(true)
                .build();
        imageMapper.updateByExampleSelective(image, example);
    }

    public void updateSharedStatusByContext(Integer contextType, Long contextId, Boolean shared) {
        ImageExample example = new ImageExample();
        example.or().andContextTypeEqualTo(contextType)
                .andContextIdEqualTo(contextId)
                .andDeletedEqualTo(false);
        Image image = Image.builder()
                .shared(shared)
                .build();
        imageMapper.updateByExampleSelective(image, example);
    }

    public Image getById(Long id) {
        Image image = imageMapper.selectByPrimaryKey(id);
        if (image == null || image.getDeleted()) {
            return null;
        }
        return image;
    }

    public List<Image> listByIds(List<Long> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return new ArrayList<>();
        }
        ImageExample example = new ImageExample();
        example.or().andIdIn(ids)
                .andDeletedEqualTo(false);
        return imageMapper.selectByExample(example);
    }

    public List<Image> listByContext(Integer contextType, Long contextId) {
        ImageExample example = new ImageExample();
        example.or().andContextTypeEqualTo(contextType)
                .andContextIdEqualTo(contextId)
                .andDeletedEqualTo(false);
        return imageMapper.selectByExample(example);
    }

    public List<Image> listTeacherVisibleImages(Long userId, Boolean excludeGpu) {
        ImageExample example = new ImageExample();
        // 公有的镜像都可以访问到
        ImageExample.Criteria criteria = example.createCriteria();
        criteria.andSharedEqualTo(true)
                .andDeletedEqualTo(false);
        if (excludeGpu) {
            criteria.andGpuEqualTo(false);
        }
        // 自己制作的镜像也可以访问到
        ImageExample.Criteria orCriteria = example.or();
        orCriteria.andImageSourceEqualTo(TEACHER_MADE_CODE)
                .andUserIdEqualTo(userId)
                .andSharedEqualTo(false)
                .andDeletedEqualTo(false);
        if (excludeGpu) {
            criteria.andGpuEqualTo(false);
        }
        return imageMapper.selectByExample(example);
    }

    public List<Image> listSharedImages(Boolean excludeGpu) {
        ImageExample example = new ImageExample();
        ImageExample.Criteria criteria = example.createCriteria();
        criteria.andSharedEqualTo(true)
                .andDeletedEqualTo(false);
        if (excludeGpu) {
            criteria.andGpuEqualTo(false);
        }
        return imageMapper.selectByExample(example);
    }

    public Boolean isExistsImageName(String imageName) {
        ImageExample example = new ImageExample();
        example.or().andImageNameEqualTo(imageName)
                .andDeletedEqualTo(false);
        List<Image> images = imageMapper.selectByExample(example);
        return CollectionUtils.isNotEmpty(images);
    }

    public Long countByContext(Long userId, Integer contextType, Long contextId) {
        ImageExample example = new ImageExample();
        example.or().andUserIdEqualTo(userId)
                .andContextTypeEqualTo(contextType)
                .andContextIdEqualTo(contextId)
                .andDeletedEqualTo(false);
        return imageMapper.countByExample(example);
    }

    public Boolean isExistsImageNameInDocker(String imageNameInDocker) {
        ImageExample example = new ImageExample();
        example.or().andImageNameInDockerEqualTo(imageNameInDocker)
                .andDeletedEqualTo(false);
        List<Image> images = imageMapper.selectByExample(example);
        return CollectionUtils.isNotEmpty(images);
    }

    public Image getByImageNameInDocker(String imageNameInDocker) {
        ImageExample example = new ImageExample();
        example.or().andImageNameInDockerEqualTo(imageNameInDocker)
                .andDeletedEqualTo(false);
        List<Image> images = imageMapper.selectByExample(example);

        if (CollectionUtils.isNotEmpty(images)) {
            return images.get(0);
        } else {
            return null;
        }
    }
}
